package com.zhiwang.planet.ui.my

import android.os.Bundle
import androidx.core.content.ContextCompat
import android.text.SpannableString
import android.view.View
import android.view.ViewGroup
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.VipRecordView
import com.zhiwang.planet.mvvm.model.entity.main.VipRecordItem
import com.zhiwang.planet.mvvm.model.entity.main.VipRecordModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.setGone
import com.zhiwang.planet.util.setVisible
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_vip_record.*
import kotlinx.android.synthetic.main.item_withdraw_record.view.*
import javax.inject.Inject

class VipRecordActivity : BaseActivity(), VipRecordView {

    @Inject
    lateinit var userPresenter: UserPresenter

    var adapter = VipRecordAdapter()

    var page = 1
    var records = ArrayList<VipRecordItem>(12)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vip_record)
        setSupportActionBar(toolbar)
        init()
        userPresenter.vipRecordView = this
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    override fun initInjection() {
        DaggerUserComponent.builder()
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .appComponent(getAppComponent())
                .build()
                .inject(this)
    }

    private fun init() {
        recyclerView.adapter = adapter
        smartRefresh.setOnRefreshListener {
            page = 1
            userPresenter.getVipRecord(page)

        }

        smartRefresh.setOnLoadMoreListener {
            userPresenter.getVipRecord(++page)
        }

        userPresenter.getVipRecord(page)
    }

    val TAG = "VipRecordActivity"
    override fun onLoadVipRecord(vipRecordModel: VipRecordModel) {
        if (vipRecordModel.list.isEmpty() && page == 1) {
            emptyView.setVisible()
            return
        }
        emptyView.setGone()
        if (smartRefresh.state == RefreshState.Refreshing || page == 1) {//刷新的话，重新拉取数据
            records.clear()
            records.addAll(vipRecordModel.list)
            adapter.notifyDataSetChanged()
            smartRefresh.setEnableLoadMore(true)
            page = 1
        } else if (smartRefresh.state == RefreshState.Loading) {
            val size = records.size
            records.addAll(vipRecordModel.list)
            val end = records.size
            adapter.notifyItemRangeInserted(size, end)
        }

        if (vipRecordModel.hasNextPage == 0) smartRefresh.setEnableLoadMore(false)
        smartRefresh.finishRefresh()
        smartRefresh.finishLoadMore()
    }

    inner class VipRecordAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<VipItemViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VipItemViewHolder {
            return VipItemViewHolder(layoutInflater.inflate(R.layout.item_withdraw_record, parent, false))
        }

        override fun getItemCount() = records.size

        override fun onBindViewHolder(viewHolder: VipItemViewHolder, position: Int) {
            viewHolder.bind()
        }

    }

    inner class VipItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {

        fun bind() {
            val data = records[adapterPosition]
            itemView.withdraw_type.text = data.vip_txt
            itemView.withdraw_date.text = data.createTime
            val spannableString = SpannableString(data.amount.plus("元"))
            itemView.withdraw_amount.text = spannableString

            val color = ContextCompat.getColor(itemView.context,
                    if (data.status != "已支付") R.color.colorOrange else R.color.colorComplete)
            itemView.withdraw_state.text = data.status
            itemView.withdraw_state.setTextColor(color)

        }
    }
}
